
cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct
use medpar_100pct_panel.dta, replace 
keep if group  == 999

local treatment_period =  mofd(mdy(12, 12, 2005))
disp `treatment_period'


** Create flat line projection
gen last12 = pmt_amt if `treatment_period' - dt <= 12 & `treatment_period' - dt > 0 
egen flatline = mean(last12)
replace flatline = . if dt < `treatment_period' - 12

** Produce difference in series
gen deterrence = flatline - pmt_amt if dt >= `treatment_period'

** Time discount the difference at 10% 
local r = 1.1^(1/12)

gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
replace deterrence_disc = . if dt-`treatment_period' >= 60
count if !mi(deterrence_disc)
assert(r(N)==60)

*** Compute total
log using robustness/flatline/deterrence_flatline.log, replace
preserve
collapse (sum) deterrence_disc
qui summarize deterrence_disc
local deterrence = r(min)
disp `deterrence'
restore
log close

*** Truncate graph at 5 years 
drop if dt  - `treatment_period' >= 60
summarize dt 
local minperiod = r(min)
local maxperiod = r(max)


** Settlement date:
local settlement= mofd(mdy(5,22,2008))


format dt %tm_Mon_YY
twoway (line pmt_amt dt, lcolor(navy) lwidth(thick)) (line flatline dt, lwidth(thick) lcolor(maroon) lpattern(dash)) (rarea pmt_amt flatline dt if dt>= `treatment_period', color(gs5)), xline(`treatment_period') xline(`settlement', lcolor(black)) legend(order(1 "Inpatient Kyphoplasty" 2 "Flat Line Projection") width(120)) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 516 `treatment_period' `settlement' 612) xtitle("Month") ytitle("$/Month") graphregion(color(white)) yscale(range(0 55000000)) ylabel(0 25000000 "$25 Mil" 50000000 "$50 Mil", format(%13.0fc)) 

graph save "robustness/flatline/KyphonOverlay_FlatLine.gph", replace
